import sys
import os

from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QVBoxLayout,
    QHBoxLayout,
    QLineEdit,
    QPushButton,
    QFileDialog,
)


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()
        h_layout = QHBoxLayout()

        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText("请输入或选择文件/文件夹路径")

        file_btn = QPushButton("选择文件")
        file_btn.clicked.connect(self.open_file_dialog)

        folder_btn = QPushButton("选择文件夹")
        folder_btn.clicked.connect(self.open_folder_dialog)

        h_layout.addWidget(self.line_edit)
        h_layout.addWidget(file_btn)
        h_layout.addWidget(folder_btn)

        layout.addLayout(h_layout)
        self.setLayout(layout)
        self.setWindowTitle("文件/文件夹路径输入框")
        self.setGeometry(300, 300, 400, 250)
        self.show()

    def open_file_dialog(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", os.getcwd())
        if file_path:
            self.line_edit.setText(file_path)

    def open_folder_dialog(self):
        folder_path = QFileDialog.getExistingDirectory(self, "选择文件夹", os.getcwd())
        if folder_path:
            self.line_edit.setText(folder_path)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
